#!/usr/bin/bash

read -p "please input the num: " num

if [[ ! "$num" =~ ^[0-9]+$ ]];then 
    echo "error number"
    exit 1
fi 

read -p "please input the prefix: " prefix
if [ -z "$prefix" ];then 
    echo "error prefix"
    exit 1
fi 

for i in `seq $num`
do
    user=$prefix$i
    useradd $user
    # 此写法，ubuntu不支持，centos写法
    # echo "123" | passwd --stdin $user &>/dev/null
    
    # 以下为ubuntu支持写法
    # echo $user:"123" | sudo chpasswd 
    echo $user:"123" | chpasswd 
    if [ $? -eq 0 ];then 
        echo "$user created."
    fi
done